#include "SceneListXml.h"

SceneListXml::SceneListXml()
{
    m_sceneMap.clear();
    m_delayTimeList.clear();
}
QMap<ControlPointStruct, QList<ControlPointStruct> > SceneListXml::sceneMap() const
{
    return m_sceneMap;
}

QList<ControlPointStruct> SceneListXml::controlPointList(const ControlPointStruct &controlPoint)
{
    return m_sceneMap.value(controlPoint);
}

QList<ControlPointStruct> SceneListXml::controlPointList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<ControlPointStruct> controlPointList;
    foreach (const ControlPointStruct &sourcePoint, sourcePointList)
    {
        controlPointList.append(this->controlPointList(sourcePoint));
    }
    return controlPointList;
}

QList<SWKNXDataStruct> SceneListXml::controlKNXList(const ControlPointStruct &controlPoint)
{
        return m_knxDataMap.value(controlPoint);
}
QList<SWKNXDataStruct> SceneListXml::controlKNXList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<SWKNXDataStruct> controlKNXList;
    foreach (const ControlPointStruct &sourcePoint, sourcePointList)
    {
        controlKNXList.append(this->controlKNXList(sourcePoint));
    }
    return controlKNXList;
}


bool SceneListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
       // qDebug()<<"reading SceneListXML";
        if(rootChild.toElement().tagName() == "ScenePT")   //解析场景点位
        {
            ControlPointStruct scenePoint;
            QStringList strTemp = rootChild.toElement().attribute("name").split(",");
            QStringList PortTemp= rootChild.toElement().attribute("Port").split(",");
            if(PortTemp.at(0)=="")
            {
                PortTemp.clear();
            }
            scenePoint.setAddress(strTemp.at(0).toInt());
            scenePoint.setValue(strTemp.at(1).toInt());
           // scenePoint.setPortList(PortTemp);
            QDomNode scenePTChild = rootChild.toElement().firstChild();
            QList<ControlPointStruct> controlPointList;
            QList<SWKNXDataStruct> controlSWKNXList;
            while(!scenePTChild.isNull())
            {
                if (scenePTChild.toElement().tagName() == "ControlPT")  //解析控制点位
                {
                    ControlPointStruct controlPoint;
                    controlPoint.clearnPortList();
                    strTemp = scenePTChild.toElement().text().split(",");
                    controlPoint.setAddress(strTemp.at(0).toInt());
                    controlPoint.setValue(strTemp.at(1).toInt());
                   // qDebug()<<"Porttemp.size"<<QString::number(PortTemp.size());
                    for(int i=0;i<PortTemp.size();i++)
                    {
                        controlPoint.JoinPortList(PortTemp.at(i).toUpper());
                    }
                    if(strTemp.size()>2)
                    {
                    controlPoint.setDelayTime(strTemp.at(2).toInt());
                   // qDebug()<<"DelayTime="<<QString::number(strTemp.at(2).toLongLong());
                    }
                    controlPointList.append(controlPoint);
                }
                else if (scenePTChild.toElement().tagName() == "ThirdPT")
                {
                    ControlPointStruct controlPoint;
                    controlPoint.clearnPortList();
                    controlPoint.setAddress(0);
                    controlPoint.setValue(0);
                    strTemp = scenePTChild.toElement().text().split(",");
                    quint8 len = strTemp.at(0).toInt();
                    if((len > 0)&&(strTemp.size()>=len+1))
                    {
                        controlPoint.setThirdFuncLen(len);
                        QByteArray list;
                        for(int i=0;i<len;i++)
                        {
                        list.append(strTemp.at(i+1).toInt(0,16));
                        }
                        controlPoint.setThirdFuncList(list);

                        if(strTemp.size() > len+1)
                        {
                             controlPoint.setDelayTime(strTemp.at(len+1).toInt());
                        }
                        controlPointList.append(controlPoint);
                    }

           }
           else if (scenePTChild.toElement().tagName() == "KNXPT")
           {
                SWKNXDataStruct swknxData;
               strTemp = scenePTChild.toElement().text().split(",");
               if((strTemp.size()==3)||(strTemp.size()==4))
               {
                   QStringList strTemp1 = strTemp.at(0).split("/");
                   QByteArray dataTemp;
                   QStringList::const_iterator it;
                    for(it=strTemp1.constBegin();it != strTemp1.constEnd();++it)
                        dataTemp.append((*it).toInt());
                    if(dataTemp.size()==3)
                    {
                     quint16 address = dataTemp.at(0)*2048+dataTemp.at(1)*256+dataTemp.at(2);
                        swknxData.setGroupAddress(address);
                       // qDebug()<<"[SceneListXml::parser##########]--"<<address;

                    }

                    if(strTemp.at(1).toInt() >= 6)
                       swknxData.setKnxLen(strTemp.at(1).toInt());


                    dataTemp.clear();

                     strTemp1 = strTemp.at(2).split("/");
                     for(it=strTemp1.constBegin();it != strTemp1.constEnd();++it)
                         dataTemp.append((*it).toInt());
                     swknxData.setDataList(dataTemp);


                     if(strTemp.size()==4)
                     swknxData.setDelayTime(strTemp.at(3).toInt());
                        else
                   swknxData.setDelayTime(0);

                   controlSWKNXList.append(swknxData);


               }


           }
            scenePTChild = scenePTChild.nextSibling();
            }
            m_sceneMap.insert(scenePoint,controlPointList);
            m_knxDataMap.insert(scenePoint,controlSWKNXList);
        }
        rootChild = rootChild.nextSibling();
    }
    qDebug()<<"runing Scendlistxml";
    return true;
}

bool SceneListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_sceneMap.clear();
    return true;
}

void SceneListXml::setDelayTimeList(const QList<ControlPointStruct> list)
{
    m_delayTimeList=list;
}

QList<ControlPointStruct> SceneListXml::getDelayTimeList()const
{
    return m_delayTimeList;
}

void SceneListXml::joinDelayTimeList(const ControlPointStruct Struct)
{
   /* int i;
    for(i=0;i<m_delayTimeList.size();i++)
    {
        ControlPointStruct _Struct=m_delayTimeList.at(i);
        if((_Struct.address()==Struct.address())&&(_Struct.value()==Struct.value()))
            return;
    }*/
    qDebug()<<"[SceneListXml::joinDelayTimeList]";
    m_delayTimeList.append(Struct);
}

